/* AMX Mod X
*   Personal Models Plugin
*
* (c) Copyright 2005 by VEN (aka VnMnL)
*    Technical idea has been taken from
*    AMX Admin Model Plugin by whitemike
*
* This file is provided as is (no warranties)
*/

#include <amxmodx>
#include <cstrike>

#define ENTRIES_NUM 32

new g_name[ENTRIES_NUM][32]
new g_names
new g_exist[ENTRIES_NUM]

public plugin_init() {
  register_plugin("Personal Models","0.2","VEN aka VnMnL")
  register_event("ResetHUD","model_change","b")
  register_cvar("amx_pmodels_casesens","0") // set to 1 to case sensitivity in names
}

public plugin_modules()
  require_module("cstrike")

public plugin_precache() {
  load_config()
  new group[32]
  for (new i=0; i<g_names; ++i) {
    new string[32]
    string=g_name[i]
    if (g_name[i][0]=='#')
      copy(group,31,g_name[i][1])
    if (group[0])
      string=group
    if (group[0] && g_name[i][0]!='#')
      continue
    new model[96]
    if (g_exist[i]&1) {
      format(model,95,"models/player/%s_t/%s_t.mdl",string,string)
      precache_model(model)
    }
    if (g_exist[i]&2) {
      format(model,95,"models/player/%s_ct/%s_ct.mdl",string,string)
      precache_model(model)
    }
  }
}

public client_infochanged(id) {
  if (!is_user_connected(id))
    return PLUGIN_CONTINUE
  new old_name[32],new_name[32]
  get_user_name(id,old_name,31)
  get_user_info(id,"name",new_name,31)
  if ((!get_cvar_num("amx_pmodels_casesens") && equali(old_name,new_name)) || equal(old_name,new_name))
    return PLUGIN_CONTINUE
  model_change(id)
  return PLUGIN_CONTINUE
}

public model_change(id) {
  if (is_user_connected(id))
    set_task(0.0,"model_change_task",id)
}

public model_change_task(id) {
  if (!is_user_connected(id))
    return PLUGIN_CONTINUE
  new group[32]
  new name[32]
  get_user_name(id,name,31)
  for (new i=0; i<g_names; ++i) {
    if (g_name[i][0]=='#') {
      copy(group,31,g_name[i][1])
      continue
    }
    new string[32]
    string=g_name[i]
    if (group[0])
      string=group
    if (equal(name,g_name[i]) || (!get_cvar_num("amx_pmodels_casesens") && equali(name,g_name[i]))) {
      new CsTeams:team=cs_get_user_team(id)
      new model[36]
      if (team==CS_TEAM_T && (g_exist[i]&1)) {
        format(model,35,"%s_t",string)
        cs_set_user_model(id,model)
        return PLUGIN_CONTINUE
      }
      else if (team==CS_TEAM_CT && (g_exist[i]&2)) {
        format(model,35,"%s_ct",string)
        cs_set_user_model(id,model)
        return PLUGIN_CONTINUE
      }
    }
  }
  cs_reset_user_model(id)
  return PLUGIN_CONTINUE
}

public load_config() {
  new config[64]
  get_localinfo("amxx_configsdir",config,63)
  format(config,63,"%s/pmodels.ini",config)
  if (!file_exists(config))
    return PLUGIN_CONTINUE
  new string[32],line,len,group[32]
  while(read_file(config,line++,string,31,len)) {
    if (string[0]==';' || !len)
      continue
    if (g_names==ENTRIES_NUM) {
      log_amx("Warning: In process of reading file ^"%s^" defined value ^"ENTRIES NUM^" has been reached",config)
      return PLUGIN_CONTINUE
    }
    g_name[g_names]=string
    if (string[0]=='#')
      copy(group,31,string[1])
    if (group[0])
      string=group
    new model[96]
    format(model,95,"models/player/%s_t/%s_t.mdl",string,string)
    if (file_exists(model))
      g_exist[g_names]|=1
    format(model,95,"models/player/%s_ct/%s_ct.mdl",string,string)
    if (file_exists(model))
      g_exist[g_names]|=2
    if (g_exist[g_names])
      g_names++
  }
  return PLUGIN_CONTINUE
}
